//
//  playerView1VC.m
//  spritetest
//
//  Created by wanglinlin on 16/7/31.
//  Copyright © 2016年 1223. All rights reserved.
//

#import "playerView1VC.h"

@interface playerView1VC ()
@property (nonatomic, strong)NSMutableArray *array;
@property (nonatomic, strong)NSMutableArray *filterStreakArray;



@end

@implementation playerView1VC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.array = [[NSMutableArray alloc]init];
    
    self.filterStreakArray = [[NSMutableArray alloc]init];
    //    MPMoviePlayerViewController *playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:nil];
    ////    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:)
    ////                                                 name:MPMoviePlayerPlaybackDidFinishNotification
    ////                                               object:[playerViewController moviePlayer]];
    //    // add to view
    //    [self.view addSubview:playerViewController.view];
    // play movie
    //    NSBundle *bundle = [NSBundle mainBundle];
    //    NSString *moviePath = [bundle pathForResource:@"supervideo" ofType:@"mp4"];
    //    NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
    //    UIView * sd =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 200)];
    //    [self.view addSubview:sd];
    //    sd.backgroundColor =[UIColor blueColor];
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *moviePath = [bundle pathForResource:@"livetrain" ofType:@"mp4"];
    NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
    NSURL *sourceMovieURL = [NSURL URLWithString:@"http://220.181.117.131/205/32/1/letv-uts/14/ver_00_22-1049007645-avc-199959-aac-32008-376880-11338499-5381f77093ddab5fa6b201d0ac6a86b3-1465270743147.m3u8?crypt=81aa7f2e214&b=240&nlh=4096&nlt=60&bf=72&p2p=1&video_type=mp4&termid=2&tss=ios&platid=3&splatid=341&its=0&qos=3&fcheck=0&mltag=4701&proxy=611247386,1778575169,1778917272&uid=1105892494&keyitem=GOw_33YJAAbXYE-cnQwpfLlv_b2zAkYctFVqe5bsXQpaGNn3T1-vhw..&ntm=1467870600&nkey=ce5416cb035d0ffb3083b18f7aef2b17&nkey2=c980c7a3d77a31273698c90143e88d76&geo=CN-1-9-666&appid=4000&cde=1023&cdeid=01aa64fd39ffb47a0de9c77a577dc0be&cdekey=fbb798ae6f858f0fd7e4cda355e7aabb&cdetm=1467859610&cvid=417001402798&dname=mobile&hwtype=iphone&iscpn=&key=955230a5962e6dd8ac61298c59ff0dbc&m3v=3&mmsid=58404825&ostype=macos&p1=0&p2=00&payff=0&pcode=010210000&pid=10022718&playid=0&sign=mb&tag=mobile&tm=1467859310&uinfo=AAAAAAAAAAB4lylV6ru-7PP5s04mvFNswabIFcmWgDSr7nIjBWEDfQ==&uuid=01563B05-AB6A-4880-9FB6-168DC34EAF89_1467859310658&version=6.7&vid=25697432&vtype=161&errc=0&gn=820&vrtmcd=102&buss=4701&cips=10.58.92.51"];
    
    AVAsset *movieAsset    = [AVURLAsset URLAssetWithURL:movieURL options:nil];
    //    NSURL *url =[NSURL  URLWithString:@"http:/127.0.0.1:10240/d2c0af6c051fe02926bdb92f18e1c9cd/movie_local.m3u8"];
    //    _player = [[MPMoviePlayerController alloc]initWithContentURL:url];
    //    _player.controlStyle = MPMovieControlStyleFullscreen;
    //    _player.shouldAutoplay = YES;
    //    _player.repeatMode = MPMovieRepeatModeOne;
    //    [_player setFullscreen:YES animated:YES];
    //    _player.scalingMode = MPMovieScalingModeFill ;
    //    [_player.view setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width)];
    //    _player.view.userInteractionEnabled =YES;
    //    [self.view addSubview:_player.view];
    //    [_player play];
    AVPlayerItem *firstVideoItem = [AVPlayerItem playerItemWithURL:movieURL];
    //    AVPlayer *player = [AVPlayer playerWithPlayerItem:firstVideoItem];
    
    self.queuePlayer = [AVQueuePlayer queuePlayerWithItems:[NSArray arrayWithObjects:firstVideoItem,nil]];
    
    AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.queuePlayer ];
    layer.player.actionAtItemEnd = AVPlayerItemStatusReadyToPlay;
    layer.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    [self.view.layer addSublayer:layer];
    //    [player play];
    [self.queuePlayer play];
    //        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //                       ^{
    //                           [self.queuePlayer play];
    //                       }
    //                       );
    //
    self.glkVideoView = [[LTGLKView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
//    self.glkVideoView.center = CGPointMake(self.view.center.y, self.view.center.x);
    self.glkVideoView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.glkVideoView.LTGLKViewDelegate = self;
    [self.glkVideoView prepareGLKView];
    [self.view addSubview: self.glkVideoView];
    [self panoramaAddGLKViewGesture:self.view];
    self.panoramaOrentation = [UIDevice currentDevice].orientation;
    [self panoramaSetupGyroscope];
    
    

    
    // Do any additional setup after loading the view.
}- (void) LTGLKViewGetPixelBuffer: (LTGLKView*) ltGLKView
{
    [self getPixelBuffer:YES
              withResult:^(CVPixelBufferRef pixelBuffer) {
                  [self.glkVideoView displayPixelBuffer: pixelBuffer];
              }];
}
- (void)getPixelBuffer:(BOOL) isPanorama withResult:(DelayReturnPixelBuffer) delayReturnPixelBuffer
{
    AVPlayerItem* currentItem = self.queuePlayer.currentItem;
    
    if (!currentItem) {
        NSLog(@"LTPlayerCore currentItem is nil");
        return;
    }
    float systemVersion = [[[UIDevice currentDevice] systemVersion] doubleValue];
    // iOS 8
    // videoOutput用了之后必须立即remove,否则退到后台再回来就没有视频画面了
    if (currentItem.outputs.count == 0 || !self.videoOutput) {
        NSDictionary* videoOutputOptions = @{ (id) kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange) };
        self.videoOutput = [[AVPlayerItemVideoOutput alloc] initWithPixelBufferAttributes: videoOutputOptions];
        [currentItem addOutput: self.videoOutput];
        // 第一次addoutput,都要等1秒
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            CVPixelBufferRef buffer = 0;
            CMTime currentTime = [self.videoOutput itemTimeForHostTime: CACurrentMediaTime ()];
            if ([self.videoOutput hasNewPixelBufferForItemTime: currentTime]) {
                buffer = [self.videoOutput copyPixelBufferForItemTime: currentTime itemTimeForDisplay: NULL];
            }
            delayReturnPixelBuffer(buffer);
            if (systemVersion >= 8 && systemVersion < 9 && !isPanorama) {
                [currentItem removeOutput:self.videoOutput];
                self.videoOutput = nil;
            }
        });
    } else {
        
        CVPixelBufferRef buffer = 0;
        CMTime currentTime = [self.videoOutput itemTimeForHostTime: CACurrentMediaTime ()];
        if ([self.videoOutput hasNewPixelBufferForItemTime: currentTime]) {
            buffer = [self.videoOutput copyPixelBufferForItemTime: currentTime itemTimeForDisplay: NULL];
        }
        delayReturnPixelBuffer(buffer);
    }
}


- (void) LTGLKViewAdditionalMovement: (LTGLKView*) ltGLKView
{
    
}

- (CGRect)screenBoundsWithPortraitOrientation{
    UIScreen *screen = [UIScreen mainScreen];
    CGRect bounds =screen.bounds;
    if([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight){
        CGRect newBounds = bounds;
        newBounds.size.width =bounds.size.height;
        newBounds.size.height =bounds.size.width;
        return newBounds;
    }else{
        return bounds;
    }
}

-(void)viewWillAppear:(BOOL)animated{
    //    [[NSNotificationCenter defaultCenter] removeObserver: self name: UIDeviceOrientationDidChangeNotification object: nil];
    //    [[NSNotificationCenter defaultCenter] addObserver: self
    //                                             selector: @selector (deviceOrientationDidChange:)
    //                                                 name: UIDeviceOrientationDidChangeNotification
    //                                               object: nil];
    //    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    
}
- (BOOL)checkPanoramaDeviceOrientationStatus
{
    if ([UIDevice currentDevice].orientation != UIDeviceOrientationLandscapeLeft &&
        [UIDevice currentDevice].orientation != UIDeviceOrientationLandscapeRight) {
        return YES;
    }
    return NO;
}

//// 处理屏幕转动
//- (void)deviceOrientationDidChange:(NSNotification *)noti {
//    NSLog(@"deviceOrientationDidChange : %@",self.class);
//    if ([UIDevice currentDevice].orientation == UIDeviceOrientationUnknown || [UIDevice currentDevice].orientation == UIDeviceOrientationFaceUp || [UIDevice currentDevice].orientation == UIDeviceOrientationFaceDown) {
//        return;
//    }
//    // 全景视频
//    if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft ||
//        [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight ||
//        [UIDevice currentDevice].orientation == UIDeviceOrientationPortrait ||
//        [UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown) {
//        self.panoramaOrentation = [UIDevice currentDevice].orientation;
//    }
////#warning 全景视频 禁用竖屏
////    if ([self checkPanoramaDeviceOrientationStatus]) {
////        return;
////    }
//    CGFloat angle;
//
//    UIInterfaceOrientation orientation;
//    UIDeviceOrientation realOrientation;
//    if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
//        orientation = UIInterfaceOrientationLandscapeRight;
//        realOrientation = UIDeviceOrientationLandscapeLeft;
////        angle = M_PI / 2;
//    }else if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait){
//           angle =M_PI / 2;
//
//    }else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight){
//
////
//    }
//    //    self.view.transform = CGAffineTransformMakeRotation(angle);
//
//    _player.view.transform = CGAffineTransformMakeRotation(angle);
//    _player.view.frame =CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) ;
//
//    [[UIApplication sharedApplication] setStatusBarOrientation: orientation animated: NO];
//
//
//}
- (void)panoramaSetupGyroscope
{
    if (self.panoramaMotionManager) {
        [self.panoramaMotionManager stopDeviceMotionUpdates];
        self.panoramaMotionManager = nil;
    }
    self.panoramaMotionManager = [[CMMotionManager alloc] init];
    if (self.isOpenGyroscope) {
        self.isOpenGyroscope = NO;
    } else {
        
        [self panoramaGyroscopeChoose];
        self.isOpenGyroscope = YES;
    }
    
}

- (void)panoramaGyroscopeChoose
{
    // LTGLKView *glkView = ((LTPanoramaPlayerCore *)self.player).glkVideoView;
    LTGLKView *glkView = self.glkVideoView;
    
    //    if (![glkView isKindOfClass:[LTGLKView class]]) {
    //        return;
    //    }
    // 开启或关闭了重力感应
    // 进来之后先调用tap
    NSLog(@"");
    [_player.view hitTest:CGPointMake(1, 1) withEvent:nil];
    if ([self.panoramaMotionManager isDeviceMotionAvailable]) {
        if (!self.panoramaMotionManager.isDeviceMotionActive) {
            glkView.prevTouchPoint = CGPointZero;
            glkView.isGyroModeActive = YES;
            
            // 这里用0.01的话 在iPhone 4上会卡,在高配机(6)上太灵活会在某一个方向卡住...不知为啥
            // 就用0.02吧
            float updateTime = 0.02f;
            self.panoramaMotionManager.deviceMotionUpdateInterval = updateTime;
            __weak typeof(glkView) weakGlkView = glkView;
            [self.panoramaMotionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
                                                            withHandler:^(CMDeviceMotion *motion, NSError *error) {
                                                                if (weakGlkView.isMoveModeActive) {
                                                                    return;
                                                                }
                                                                
                                                                /*NSLog(@"panoramaOrentation.................:%ld", (long)self.panoramaOrentation);
                                                                 NSLog(@"UIDeviceOrientationLandscapeRight:%ld", (long)UIDeviceOrientationLandscapeRight);
                                                                 NSLog(@"UIDeviceOrientationLandscapeLeft:%ld", (long)UIDeviceOrientationLandscapeLeft);
                                                                 */
                                                                //                                                        float x = -(motion.rotationRate.x * 5);
                                                                //                                                        float y = (motion.rotationRate.y * 5);
                                                                
                                                                float x = (motion.rotationRate.x * 5);
                                                                float y = (motion.rotationRate.y * 5);
                                                                float z = (motion.rotationRate.z * 3);
                                                                //                                                        NSLog(@"z:%f", motion.rotationRate.z);
                                                                //                                                        NSLog(@"self.panoramaOrentation:%ld", self.panoramaOrentation);
                                                                
                                                                if (self.panoramaOrentation == UIDeviceOrientationLandscapeRight) {
                                                                    x = -(motion.rotationRate.x * 5);
                                                                    y = (motion.rotationRate.y * 5);
                                                                } else if (self.panoramaOrentation == UIDeviceOrientationLandscapeLeft) {
                                                                    x = (motion.rotationRate.x * 5);
                                                                    y = -(motion.rotationRate.y * 5);
                                                                } else if (self.panoramaOrentation == UIDeviceOrientationPortrait) {
                                                                    //                                                            NSLog(@" ............... UIDeviceOrientationPortrait");
                                                                    return;
                                                                } else if (self.panoramaOrentation == UIDeviceOrientationPortraitUpsideDown) {
                                                                    // 竖屏
                                                                    //                                                            NSLog(@" ............... UIDeviceOrientationPortraitUpsideDown");
                                                                    //                                                            [self moveToPointXFromMotion:y
                                                                    //                                                                               andPointY:x
                                                                    //                                                                               andPointZ:z];
                                                                    return;
                                                                }
                                                                [self moveToPointXFromMotion:x
                                                                                   andPointY:y
                                                                                   andPointZ:z];
                                                            }];
        } else {
            [self.panoramaMotionManager stopDeviceMotionUpdates];
            glkView.isGyroModeActive = NO;
        }
    } else {
        [self showAlertNoGyroscopeAvaliable];
        
        glkView.isGyroModeActive = NO;
    }
}
- (void)showAlertNoGyroscopeAvaliable
{
    NSString *title = [[[NSBundle mainBundle] localizedInfoDictionary] objectForKey:@"CFBundleDisplayName"];
    [[[UIAlertView alloc] initWithTitle:title message:NSLocalizedString(@"当前设备不支持陀螺仪", @"") delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
}





- (void)panoramaAddGLKViewGesture:(UIView *)playControlView {
    
    for(int i=0; i<[playControlView.gestureRecognizers count]; i++) {
        UIGestureRecognizer *gesture = [playControlView.gestureRecognizers objectAtIndex:i];
        [playControlView removeGestureRecognizer:gesture];
    }
    
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(panoramaPinchForZoom:)];
    pinch.delegate = self;
    [playControlView addGestureRecognizer:pinch];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(panoramaTapGesture)];
    tapGesture.delegate = self;
    [playControlView addGestureRecognizer:tapGesture];
    
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panoramaPanGesture:)];
    panGesture.delegate = self;
    panGesture.maximumNumberOfTouches = 1;
    [playControlView addGestureRecognizer:panGesture];
}
#pragma mark - Touches

- (void)moveToPointX:(CGFloat)pointX andPointY:(CGFloat)pointY
{
    // LTGLKView *glkView = ((LTPanoramaPlayerCore *)self.player).glkVideoView;
    LTGLKView *glkView = self.glkVideoView;
    
    if (glkView.isGyroModeActive) {
        glkView.isTouchWhenGyroModeActive = YES;
    }
    pointX *= 0.005;
    pointY *= 0.005;
    
    GLKMatrix4 rotatedMatrix = GLKMatrix4MakeRotation(-pointX / glkView.zoomValue, 0, 1, 0);
    glkView.currentProjectionMatrix = GLKMatrix4Multiply(glkView.currentProjectionMatrix, rotatedMatrix);
    
    float tempM6 = glkView.modelViewProjectionMatrix.m[6];
    if ((tempM6 < 1 && tempM6 > -1) || self.continueMoveY) {
        GLKMatrix4 cameraMatrix = GLKMatrix4MakeRotation(-pointY / glkView.zoomValue, 1, 0, 0);
        glkView.cameraProjectionMatrix = GLKMatrix4Multiply(glkView.cameraProjectionMatrix, cameraMatrix);
    }
}
- (void)moveToPointXFromMotion:(CGFloat)pointX andPointY:(CGFloat)pointY andPointZ:(CGFloat)pointZ
{
    // LTGLKView *glkView = ((LTPanoramaPlayerCore *)self.player).glkVideoView;
    LTGLKView *glkView = self.glkVideoView;
    
    //    NSLog(@"pointX:%f, pointY:%f", pointX, pointY);
    if (glkView.isGyroModeActive) {
        glkView.isTouchWhenGyroModeActive = YES;
    }
    pointX *= 0.005;
    pointY *= 0.005;
    pointZ *= 0.005;
    
    GLKMatrix4 rotatedMatrix = GLKMatrix4MakeRotation(-pointX / glkView.zoomValue, 0, 1, 0);
    glkView.currentProjectionMatrix = GLKMatrix4Multiply(glkView.currentProjectionMatrix, rotatedMatrix);
    
    GLKMatrix4 cameraMatrix = GLKMatrix4MakeRotation(-pointY / glkView.zoomValue, 1, 0, 0);
    glkView.cameraProjectionMatrix = GLKMatrix4Multiply(glkView.cameraProjectionMatrix, cameraMatrix);
    
    /*
     GLKMatrix4 rotatedZMatrix = GLKMatrix4MakeRotation(-pointZ / glkView.zoomValue, 0, 0, 1);
     glkView.cameraProjectionMatrix = GLKMatrix4Multiply(glkView.cameraProjectionMatrix, rotatedZMatrix);
     */
}

#pragma mark - GestureActions

- (void)panoramaPinchForZoom:(UIPinchGestureRecognizer *)gesture
{
    //LTGLKView *glkView =((LTPanoramaPlayerCore *)self.player).glkVideoView;
    LTGLKView *glkView = self.glkVideoView;
    
    if (gesture.state == UIGestureRecognizerStateBegan) {
        glkView.isZooming = YES;
        gesture.scale = glkView.zoomValue;
    } else if (gesture.state == UIGestureRecognizerStateChanged) {
        CGFloat zoom = gesture.scale;
        zoom = MAX(MIN(zoom, kMaximumZoomValue), glkView.planetMode ? kMinimumLittlePlanetZoomValue : kMinimumZoomValue);
        glkView.zoomValue = zoom;
    } else if (gesture.state == UIGestureRecognizerStateEnded) {
        glkView.isZooming = NO;
    }
}

- (void)panoramaTapGesture
{
    //    [self.viewPlayControl forbidControlHiddenState:!self.viewPlayControl.controlIsHidden];
}

- (void)panoramaPanGesture:(UIPanGestureRecognizer *)panGesture
{
    // LTGLKView *glkView = ((LTPanoramaPlayerCore *)self.player).glkVideoView;
    LTGLKView *glkView = self.glkVideoView;
    
    CGPoint currentPoint = [panGesture locationInView:panGesture.view];
    //    NSLog(@"panGesture.view:%@", panGesture.view);
    //    NSLog(@"panGesture ............. x:%f, y:%f", currentPoint.x, currentPoint.y);
    switch (panGesture.state) {
        case UIGestureRecognizerStateEnded: {
            glkView.isMoveModeActive = NO;
            glkView.prevTouchPoint = CGPointZero;
            glkView.velocityValue = [panGesture velocityInView:panGesture.view];
            break;
        }
        case UIGestureRecognizerStateBegan: {
            glkView.isMoveModeActive = YES;
            glkView.prevTouchPoint = currentPoint;
            [self disableExtraMovement];
            break;
        }
        case UIGestureRecognizerStateChanged: {
            CGPoint point = [self calculateVerticesY:currentPoint];
            [self moveToPointX:point.x
                     andPointY:point.y];
            glkView.prevTouchPoint = currentPoint;
            break;
        }
        default:
            break;
    }
}

- (CGPoint)calculateVerticesY:(CGPoint)point
{
    // LTGLKView *glkView = ((LTPanoramaPlayerCore *)self.player).glkVideoView;
    LTGLKView *glkView = self.glkVideoView;
    
    //    for (int i=0; i<sizeof(_glkVideoView.modelViewProjectionMatrix.m) / sizeof(int); i++) {
    //        float temp = _glkVideoView.modelViewProjectionMatrix.m[i];
    //        NSLog(@"pos: %d, value: %f", i, temp);
    //    }
    
    float tempM6 = glkView.modelViewProjectionMatrix.m[6];
    float moviePointY = point.y - glkView.prevTouchPoint.y;
    float moviePointX = point.x - glkView.prevTouchPoint.x;
    //    NSLog(@"tempM6:%f", tempM6);
    //    NSLog(@"moviePointY:%f", moviePointY);
    self.continueMoveY = YES;
    if (point.y > glkView.prevTouchPoint.y) {
        // 手指向下滑,屏幕往上滚
        //        NSLog(@"movie to top");
        // 检测是否到达最顶端
        if (tempM6 >= 0.8 && moviePointY >= 0) {
            //            self.continueMoveY = NO;
        }
    } else {
        // 手指向上滑,屏幕往下滚
        //        NSLog(@"movie to bottom");
        if (tempM6 <= -1 && moviePointY <= 0) {
            self.continueMoveY = NO;
        }
    }
    return CGPointMake(moviePointX, moviePointY);
}

#pragma mark - Velocity

// 手指移动结束之后的惯性移动
- (void)setAdditionalMovement
{
    // LTGLKView *glkView =((LTPanoramaPlayerCore *)self.player).glkVideoView;
    LTGLKView *glkView = self.glkVideoView;
    
    glkView.prevTouchPoint = CGPointZero;
    glkView.velocityValue = CGPointMake(0.9 * glkView.velocityValue.x, 0.9 * glkView.velocityValue.y);
    CGPoint nextPoint = CGPointMake(kAdditionalMovementCoef * glkView.velocityValue.x, kAdditionalMovementCoef * glkView.velocityValue.y);
    // 每次惯性移动结束后递减,直到x和y<0.1就结束
    if (fabs(nextPoint.x) < 0.1 && fabs(nextPoint.y) < 0.1) {
        glkView.velocityValue = CGPointZero;
    }
    CGPoint point = [self calculateVerticesY:nextPoint];
    [self moveToPointX:point.x andPointY:point.y];
}

/**
 *  手势触发的时候清空加速度
 */
- (void)disableExtraMovement
{
    //LTGLKView *glkView = ((LTPanoramaPlayerCore *)self.player).glkVideoView;
    LTGLKView *glkView = self.glkVideoView;
    
    glkView.velocityValue = CGPointZero;
}


#pragma mark - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    //    NSLog(@" ................ touch.view:%@", touch.view);
    if ([touch.view isKindOfClass:[self class]]
        ) {
        [self disableExtraMovement];
        return YES;
    } else {
        return NO;
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
        return YES;
    }
    return NO;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
